<?php
class session{
    private $user       = '';
    private $pwd        = '';
    private $typeUser   = '';
    private $cPr        = '';
    private $login      = false;
    public function __construct($user=false,$pwd=false) {
     
        $this->user = $user;
        $this->pwd  = $pwd;
    
        session_start();
     
      
    }
    public function verificarLogin(){
      try{
            $this->login = $this->getSessVar('login');
            if(!isset( $_SESSION["login"] )  && !$this->login){
                header("location:mensaje.html");
                exit();
            }
      }catch (Exception $e) {
        return array("status"=>false,"msg"=>$e->getMessage());
      }
    }
    public function logIn(){
       try{
            require("../config/config.php");
            require("BD.php");
            $bd = new dbC($config);
            $qU = "select usuario,tipousuario,cprofesional from usuarios where usuario = '$this->user' and clave= '$this->pwd'; ";
            $rS = $bd->query($qU);
            if( $rS[ "status" ] ){
              if( $rS['total'] < 1 ){
               header('location:../index.php?attempt=true');
              }
              else{
                $this->typeUser   = $rS["rows"][0]['tipousuario'];
                $this->cPr        = $rS["rows"][0]['cprofesional'];
                $this->setSessVar('usuario',$this->user);
                $this->setSessVar('tipo',$this->typeUser);
                $this->setSessVar('cedula',$this->cPr);
                $this->setSessVar('login',true);
                header('location:../home.php');
              }
            }
            else{
              header('location:../index.php?attempt=true');
            }
            $db->commit();
            return true;  
          }
          catch(Exception $e){
            return array('status'=>false,'msg'=>$e->getMessage());
          }
    }
    public function logOut(){
        $_SESSION = array();
        session_destroy();
        header('location:../index.php');
    }

    private function setSessVar($key,$value){
      $_SESSION[$key] = $value;
    }
    private function getSessVar($key){
      return $_SESSION[$key];
    }
}
?>